<template>
	<view class="page staff bgf7 c00">
		<view class="flex flexC flexB py14 bgff">
			<picker class="flex1 aliC" mode="selector" :range="role" @change="selectRole">
				<view>
					<text>{{role[currentIndex]}}</text>
					<image class="ml4 w12" src="/static/img/icon/down.png" mode="widthFix"></image>
				</view>
			</picker>
			<view class="flex1 aliC" @click="showShop">
				<view>
					<text>{{selectShop.name}}</text>
					<image class="ml4 w12" src="/static/img/icon/down.png" mode="widthFix"></image>
				</view>
			</view>
		</view>
		<scroll-view scroll-y="true" class="list">
			<view @click="clickDetail(item)" v-for="(item,index) in list" :key="index"
				class="bgff flex flexC m12 r7 pt24 pb20 px12 shadow">
				<view class="flex1">
					<view class="flex flexC mb6">
						<text class="">{{item.username}}</text>
						<text class="ml20">{{item.mobile}}</text>
					</view>
					<view class="">
						<text class="fs10 bgf6 r40 px8 py2">{{item.role}}</text>
					</view>
				</view>
				<text class="arrowR p1"></text>
			</view>
		</scroll-view>
		<view class="add r50 bged aliC" @click="addStaff">添加</view>



		<uni-popup ref="pop" type="bottom">
			<view class="popup-content2" >
				<button @click="clickDel()" class="popup-btn button" >删除</button>
				<view class="popup-btn" @click="$refs.pop.close()">取消</view>
			</view>

		</uni-popup>

		<!-- 店铺选择 -->
		<uni-popup ref="shopPicker" type="bottom">
			<shop-list @search="search" :list="shopList" @onSelectShop="onSelectShop" @close="onClose"></shop-list>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				currentIndex: 0,
				role: ['全部员工', '保洁员', '店长'],
				list: [],
				shopList: [],
				selectShop: null,
				currentStaff:null
			};
		},
		onLoad(option) {
			var select = option.selectShop;
			if (select != "") {
				this.selectShop = JSON.parse(select);
			}
			this.getShopList();
		},
		onShow() {
			this.getList();
		},

		methods: {
			
			clickDel(){
				
				this.doDeleteStaff();
				
				
			},
			
			addStaff(){
				uni.navigateTo({
					url:'../staff/addStaff?selectShop='+JSON.stringify(this.selectShop)
				})
			},

			clickDetail(item) {
				this.$refs.pop.open();
				
				this.currentStaff = item;
			},
			
			doDeleteStaff(){
				
				this.$refs.pop.close();

				let param = {
					shop_id: this.selectShop.id,
					mobile: this.currentStaff.mobile
				
				};
				let that = this;
				this.$api.deleteStaff(param).then(res => {
				
					console.info(JSON.stringify(res));
					if (res.code == 1) {
						that.getList();
					} else {
				
					}
				})
			},
			

			selectRole(e) {
				var index = e.detail.value;
				this.currentIndex = index;
				this.role_txt = this.role[index];
				if (index == 0) {
					this.role_txt = "";
				}
				this.getList();
			},


			getList() {
				let param = {
					shop_id: this.selectShop.id,
					role: this.role_txt

				};
				let that = this;
				this.$api.staffList(param).then(res => {

					console.info(JSON.stringify(res));
					if (res.code == 1) {
						that.list = res.data;
					} else {

					}
				})
			},

			onClose() {
				this.$refs.shopPicker.close();
			},
			getShopList() {
				let param = {
					keyword: this.keyword
				};
				let that = this;
				this.$api.getShopList(param).then(res => {

					console.info(JSON.stringify(res));
					if (res.code == 1) {
						that.shopList = res.data;
						if (that.shopList.length > 0) {

						}
					} else {

					}
				})
			},

			showShop() {
				this.$refs.shopPicker.open();

			},
			search(value) {

				this.keyword = value;
				this.getShopList();
			},

			onSelectShop(item) {
				this.selectShop = item;
				this.$refs.shopPicker.close();
				this.getList();

			},
		}
	}
</script>

<style lang="scss" scoped>
	.staff {
		.w12 {
			width: 24rpx;
		}

		.w82 {
			width: 164rpx;
			height: 164rpx;
		}

		.w72 {
			width: 144rpx;
			display: inline-block;
		}

		.list {
			height: calc(100vh - 96rpx);
		}

		.add {
			position: fixed;
			bottom: 100px;
			right: 10rpx;
			width: 80rpx;
			height: 80rpx;
			line-height: 80rpx;
			border: 2rpx solid #418FF4;
		}
	}


	.popup-content2 {
		background: #FFFFFF;

		.popup-btn {
			width: 100%;
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			border-bottom: 1px solid #f5f5f5;
			background: #FFFFFF;
		}

		.button {
			width: 100%;
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			border-bottom: 1px solid #f5f5f5;
			background: #FFFFFF;
			font-size: 11pt;
		}

		button::after {
			border: none;
		}

		.pt20 {
			border-bottom: 20rpx solid #f5f5f5;
		}
	}
</style>
